package yancy.modules.sys.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import yancy.common.utils.R;
import yancy.modules.sys.controller.SysAuthorController;
import yancy.modules.sys.entity.Foo;
import yancy.modules.sys.entity.SysAuthor;
import yancy.modules.sys.service.FooService;

@Service
@Slf4j
public class DefaultFooService implements FooService {
    @Override
    public Foo getFoo(String fooName) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Foo getFoo(String fooName, String barName) {
        throw new UnsupportedOperationException();
    }

    @Override
    @Transactional
    public void insertFoo(Foo foo) {

        log.info(getClass().toString());
        try {
            Object resp = doTransaction();
            log.info(resp.toString());
        } catch (UnsupportedOperationException e) {
            log.error("unsupport operation: ", e);
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }

    @Autowired
    private SysAuthorController authorController;

    public Object doTransaction() {
        boolean success = false;
        for (int i = 0; i < 10; i++) {
//            if (i == 5){
//                throw new UnsupportedOperationException();
//            }
            try {
                SysAuthor sysAuthor = new SysAuthor();
                sysAuthor.setUsername("name"+i);
                sysAuthor.setEmail("email:"+i);
                success = authorController.yummyMethod(sysAuthor);
            } catch (UnsupportedOperationException e) {
                log.error("doTransaction operation: ", e);
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        return success;
    }

    public void throwException() {
        throw new UnsupportedOperationException();
    }

    @Override
    public void updateFoo(Foo foo) {
        throw new UnsupportedOperationException();

    }
}
